package cc.linker.steplink.crm.merchanth5.service.redis;

import cc.linker.steplink.crm.merchanth5.dao.MallBoomDao;
import cc.linker.steplink.crm.merchanth5.dao.SystemConfigDao;
import cc.linker.steplink.crm.merchanth5.dao.acs.PurchaseProcessDao;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @Author: CKQ
 * @DateTime: 2020/6/12 9:52
 * @Description: TODO
 */
@Service
public class SystemConfigService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SystemConfigDao systemConfigDao;
    @Autowired
    private MallBoomDao mallBoomDao;
    @Autowired
    private PurchaseProcessDao purchaseProcessDao;

    private static String SYS_CONFIG_KEY = "sys_config:org_pay_mode_";

    //返润比例
    public static final String ORG_PAY_KEY = "orgPay:org_pay_";
    //抽成比例
    public static final String ORG_SET_INFO_KEY = "orgSet:org_set_";

    //对应机构的支付模式
    public Integer getOrgPayModeByOrgId(String orgId) {
        String payModeInfoStr = stringRedisTemplate.opsForValue().get(SYS_CONFIG_KEY + orgId);
        if (StringUtils.isEmpty(payModeInfoStr)) {
            Integer payMode = systemConfigDao.getPayMode(orgId);
            if (payMode != null) {
                stringRedisTemplate.opsForValue().set(SYS_CONFIG_KEY + orgId, payMode.toString());
            }
            return payMode;
        } else {
            return Integer.valueOf(payModeInfoStr);
        }
    }

    //获取机构的反润配置
    public Map<String, Integer> getOrgPaySetting(String orgId) {
        // 返润模式
        Map<String, Integer> reverseProfitModel = null;
        String reverseProfitModelString = stringRedisTemplate.opsForValue().get(ORG_SET_INFO_KEY + orgId);
        if (!StringUtils.isEmpty(reverseProfitModelString)) {
            reverseProfitModel = JSON.parseObject(reverseProfitModelString, Map.class);
        } else {
            reverseProfitModel = mallBoomDao.getReverseProfitModel(orgId);
            stringRedisTemplate.opsForValue().set(ORG_SET_INFO_KEY + orgId, JSON.toJSONString(reverseProfitModel, SerializerFeature.WriteMapNullValue));
        }
        return reverseProfitModel;
    }

    /*
    * 获取机构反润信息
    * */
    public Map<String, String> getPlatformRateMap(String orgId){
        Map<String, String> platformRateMap = null;
        String platformRateMapString = stringRedisTemplate.opsForValue().get(ORG_PAY_KEY + orgId);
        if (!StringUtils.isEmpty(platformRateMapString)) {
            platformRateMap = JSON.parseObject(platformRateMapString, Map.class);
        } else {
            platformRateMap = purchaseProcessDao.getPlatformRateByOrgId2(orgId);
            stringRedisTemplate.opsForValue().set(ORG_PAY_KEY + orgId, JSON.toJSONString(platformRateMap, SerializerFeature.WriteMapNullValue));
        }
        return  platformRateMap;
    }
}
